package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-07-22
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private  CategoryBrandMapper categoryBrandMapper;
    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
       List<CategoryBrand> list = categoryBrandMapper.selectCategoryBrandList();
        return list;
    }

    @Override
    public int addCategoryBrand(CategoryBrand categoryBrand) {
        //判断分类品牌是否重复
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        Long count = categoryBrandMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new ServiceException("该分类下已有该品牌");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }
}
